﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ADA.Data;
using ADA.Entity;
using System.Data.SqlClient;
using System.Data;

namespace ADA.Business
{
    public class BOLAccount
    {
        public bool Login(string name, string password, out Admin ad, out string message)
        {
            message = "";
            ad = new Admin();
            DALAdmin dalAdmin = new DALAdmin();
            try
            {
                ad = dalAdmin.getAdminInfo(name);
                if (ad.ID == 0)
                {
                    message = Common.MESSAGE_ERROR_ACCOUNT_NOT_EXIST;
                    return false;
                }
                if (ad.Password.Equals(password) == false)
                {
                    message = Common.MESSAGE_ERROR_PASSWORD_INCORECT;
                    return false;
                }
                ad.Name = name;
                return true;
            }
            catch (SqlException)
            {
                message = Common.MESSAGE_ERROR_SQL_FAIL;
                return false;
            }
            catch(Exception)
            {
                message = Common.MESSAGE_ERROR_CONNECT_DATABASE_FAIL;
                return false;
            }
        }
    
        public bool changePass( Admin ad, string oldPass, string newPass, out string message)
        {
            message = "";
            DALAdmin dalAdmin = new DALAdmin();
            if (ad.Password.Equals(oldPass) == false)
            {
                message = Common.MESSAGE_ERROR_PASSWORD_INCORECT;
                return false;
            }
            try
            {
                dalAdmin.updatePass(ad.ID, newPass);
                return true;
            }
            catch (SqlException)
            {
                message = Common.MESSAGE_ERROR_SQL_FAIL;
                return false;
            }
            catch (Exception)
            {
                message = Common.MESSAGE_ERROR_CONNECT_DATABASE_FAIL;
                return false;
            }
        }
    
        public bool viewAllAdmin(out DataTable tblAdmin ,out string message)
        {
            tblAdmin = new DataTable();
            message = "";
            DALAdmin dalAdmin = new DALAdmin();
            try
            {
                tblAdmin = dalAdmin.getAllAdmin();
                return true;
            }
            catch (SqlException)
            {
                message = Common.MESSAGE_ERROR_SQL_FAIL;
                return false;
            }
            catch (Exception)
            {
                message = Common.MESSAGE_ERROR_CONNECT_DATABASE_FAIL;
                return false;
            }
        }

        public bool viewAllUser(out DataTable tblUser, out string message)
        {
            tblUser = new DataTable();
            message = "";
            DALUser dalUser = new DALUser();
            try
            {
                tblUser = dalUser.getAllUser();
                return true;
            }
            catch (SqlException)
            {
                message = Common.MESSAGE_ERROR_SQL_FAIL;
                return false;
            }
            catch (Exception)
            {
                message = Common.MESSAGE_ERROR_CONNECT_DATABASE_FAIL;
                return false;
            }
        }

        public bool addAdmin(Admin ad, out string message)
        {
            message = "";
            DALAdmin dalAdmin = new DALAdmin();
            try
            {
                dalAdmin.insertAdmin(ad);
                return true;
            }
            catch (SqlException)
            {
                message = Common.MESSAGE_ERROR_SQL_FAIL;
                return false;
            }
            catch (Exception)
            {
                message = Common.MESSAGE_ERROR_CONNECT_DATABASE_FAIL;
                return false;
            }
        }

        public bool deleteAdmin(short adminID, out string message)
        {
            message = "";
            DALRice dalRice = new DALRice();
            DALNews dalNews = new DALNews();
            DALAdmin dalAdmin = new DALAdmin();
            try
            {
                Admin superAd = dalAdmin.getAdminInfo("superadmin");
                dalRice.updatePoster(adminID, superAd.ID);
                dalNews.updatePoster(adminID, superAd.ID);
                dalAdmin.deleteAdmin(adminID);
                return true;
            }
            catch (SqlException)
            {
                message = Common.MESSAGE_ERROR_SQL_FAIL;
                return false;
            }
            catch (Exception)
            {
                message = Common.MESSAGE_ERROR_CONNECT_DATABASE_FAIL;
                return false;
            }
        }

        public bool viewUserDetail(long userID,out DataTable tbluser, out string message)
        {
            tbluser = new DataTable();
            message = "";
            DALUser dalUser = new DALUser();
            try
            {
                tbluser = dalUser.getUserInfo(userID);
                return true;
            }
            catch (SqlException)
            {
                message = Common.MESSAGE_ERROR_SQL_FAIL;
                return false;
            }
            catch (Exception)
            {
                message = Common.MESSAGE_ERROR_CONNECT_DATABASE_FAIL;
                return false;
            }
        }

        public bool deleteUser(long userID, out string message)
        {
            message = "";
            DALBreachReport dalReport = new DALBreachReport();
            DALWarehouse dalWh = new DALWarehouse();
            DALOrder dalOrder = new DALOrder();
            DALPost dalPost = new DALPost();
            DALUser dalUser = new DALUser();
            try
            {
                dalReport.deleteReportByUserID(userID);
                dalWh.deleteWarehouseByUserID(userID);
                dalOrder.deleteOrderByUserID(userID);
                dalOrder.deleteOrderToUserPost(userID);
                dalPost.deletePostByUserID(userID);
                dalUser.deleteUser(userID);
                return true;
            }
            catch (SqlException)
            {
                message = Common.MESSAGE_ERROR_SQL_FAIL;
                return false;
            }
            catch (Exception)
            {
                message = Common.MESSAGE_ERROR_CONNECT_DATABASE_FAIL;
                return false;
            }
        }
    }
}
